package com.emailManager;


import javax.activation.DataSource;
import javax.activation.FileDataSource;
import java.io.*;

/**
 * Created by Samuel on 2016/10/27.
 */
public class SEmail {



    public boolean sendMailForResetPassword(String toAddres, String title, String content, File file) {
        try {
            SMailModule mailInfo = new SMailModule();

            mailInfo.setMailServerHost(ControlPro.MailServerHost);
            mailInfo.setMailServerPort(ControlPro.MailServerPort);
            mailInfo.setValidate(true);

            mailInfo.setUserName(ControlPro.EmailName);
            mailInfo.setPassword(ControlPro.EmailPassword);// 您的邮箱密码

            mailInfo.setFromAddress(ControlPro.EmailName);
            mailInfo.setToAddress(toAddres);
            mailInfo.setSubject(title);
            String str = "<div style=\"height: 90px;\">欢迎您使用isOOps后台服务</div>" +
                    "<div style=\"height: 90px;\">这里是您重置密码的请求，以下是您的重置验证码：\n</div>" +
                    "<div style=\"height: 90px;\">"+content+"</div>" +
                    "<img src=\"cid:image\" style=\"width: 100px; height:100px;background-color: red;\">" +
                    "<img src=\"http://pic.qiantucdn.com/58pic/15/35/32/71j58PICmvg_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center\">"+
                    "<div style=\"height: 90px;\">验证码当天奏效</div>";
            System.out.println(SEmail.class.getResource(""));//得到当前类的classpath目录
            String strs = readFileContent("src/main/webapp/emailRej.html");
            strs = strs.replace("{image}","\"cid:image\"")
                    .replace("{name}","samuelliuse")
                    .replace("{time}","2017-9-8 15:25:00")
                    .replace("{compName}","isoops company");

            System.out.print(strs);

            mailInfo.setContent(strs);
            // 这个类主要来发送邮件
            SMailSender sms = new SMailSender();
//            DataSource fds = new FileDataSource(file);
//            sms.setImageData(fds);
            sms.sendHtmlMail(mailInfo);// 发送html格式
//            ThreadEmail threadEmail = new ThreadEmail(sms,mailInfo);
//            threadEmail.start();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    private static String readFileContent(String fileName) throws IOException {
        File file = new File(fileName);
        BufferedReader bf = new BufferedReader(new FileReader(file));
        String content = "";
        StringBuilder sb = new StringBuilder();
        while(content != null){
            content = bf.readLine();
            if(content == null){
                break;
            }
            sb.append(content.trim());
        }
        bf.close();
        return sb.toString();
    }
}

class ThreadEmail extends Thread {
    private SMailSender sender;
    private SMailModule module;
    public ThreadEmail(SMailSender sender,SMailModule module) {
        this.sender = sender;
        this.module = module;
    }


    public void run(){
        System.out.println(111111);
        this.sender.sendHtmlMail(this.module);// 发送html格式
    }
}